// components/realm/index.js
import {FenceGroup} from "../models/fence-group";
import {Judger} from "../models/judger";
import number from "../../miniprogram_npm/lin-ui/common/async-validator/validator/number";
import {Spu} from "../../models/spu";
import {Cart} from "../models/cart";

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        spu: Object,
        orderWay: String
    },

    /**
     * 组件的初始数据
     */
    data: {
        fences: Array,
        judger: Object,
        previewImg: String,
        title: String,
        price: String,
        discountPrice: String,
        stock: Number,
        noSpec: Boolean,
        skuIntact: Boolean,
        currentValues: Array,
        missingKeys: Array,
        outStock: Boolean,
        currentSkuCount: Cart.SKU_MIN_COUNT
    },

    lifetimes: {
        attached() {

        },
        ready() {
        },
        created() {

        }
    },

    observers: {
        'spu': function (spu) {
            if (!spu) { // 可能第一次请求还没有加载数据就执行了，第二次才真正加载数据，这个判断条件不能少
                return
            }
            if (Spu.isNoSpec(spu)) {
                this.processNoSpec(spu)
            } else {
                this.processHasSpec(spu)
            }
            this.triggerSpecEvent()
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        processNoSpec(spu) {
            this.setData({
                noSpec: true
            })
            this.bindSkuData(spu.skuList[0])
            this.setStockStatus(spu.sku_list[0].stock, this.data.currentSkuCount)
            //因为无规格，所以不需要规格初始化;  为了统一代码逻辑，至少有一个sku，无规格：有一个sku，但sku内部没有规格
        },
        processHasSpec(spu) {
            const fenceGroup = new FenceGroup(spu);
            fenceGroup.initFences()
            const judger = new Judger(fenceGroup)
            this.data.judger = judger
            const defaultSku = fenceGroup.getDefaultSku()
            if (defaultSku) {
                this.bindSkuData(defaultSku);
                this.setStockStatus(defaultSku.stock, this.data.currentSkuCount)
            } else {
                this.bindSpuData()
            }
            this.bindTipData()
            this.bindFenceGroupData(fenceGroup);
        },
        bindSpuData() {
            const spu = this.properties.spu
            this.setData({
                previewImg: spu.img,
                title: spu.title,
                price: spu.price,
                discountPrice: spu.discount_price,
            })
        },
        bindSkuData(sku) {
            this.setData({
                previewImg: sku.img,
                title: sku.title,
                price: sku.price,
                discountPrice: sku.discount_price,
                stock: sku.stock,
            })
        },
        bindTipData() {
            this.setData({
                skuIntact: this.data.judger.isSkuIntact(),
                currentValues: this.data.judger.getCurrentValues(),
                missingKeys: this.data.judger.getMissingKeys()
            })
        },
        bindFenceGroupData(fenceGroup) {
            this.setData({
                fences: fenceGroup.fences
            })
        },

        setStockStatus(stock, currentCount) {
            this.setData({
                outStock: this.isOUtOfStock(stock, currentCount)
            })
        },

        isOUtOfStock(stock, currentCount) {
            return stock < currentCount
        },
        switchSku(judger) {
            if (judger.isSkuIntact()) {
                const skuCode = judger.skuPending.getSkuCode()
                this.properties.spu.sku_list.forEach(sku => {
                    if (skuCode === sku.code.substring(2)) {
                        this.bindSkuData(sku)
                        this.setStockStatus(sku.stock, this.data.currentSkuCount)
                        return
                    }
                })
            }
        },
        getDeterminateSku(judger) {
            if (judger.isSkuIntact()) {
                let currentSku = null
                const skuCode = judger.skuPending.getSkuCode()
                this.properties.spu.sku_list.forEach(sku => {
                    if (skuCode === sku.code.substring(2)) {
                        currentSku = sku
                        return
                    }
                })
                return currentSku
            }
        },

        triggerSpecEvent() {
            const noSpec = Spu.isNoSpec(this.properties.spu)
            if (noSpec) {
                this.triggerEvent('specchange', {
                    noSpec
                });
            } else {
                this.triggerEvent('specchange', {
                    noSpec,
                    skuIntact: this.data.judger.isSkuIntact(),
                    currentValues: this.data.judger.getCurrentValues(),
                    missingKeys: this.data.judger.getMissingKeys()
                });
            }
        },

        onSelectCount(event) {
            const currentCount = event.detail.count
            this.data.currentSkuCount = currentCount
            const judger = this.data.judger
            if (judger.isSkuIntact()) {
                const sku = this.getDeterminateSku(judger)
                this.setStockStatus(sku.stock, currentCount)
            }
        },

        onCellTap(event) {
            const judger = this.data.judger
            judger.judge(event.detail)
            this.switchSku(judger)
            this.bindTipData()
            this.bindFenceGroupData(judger.fenceGroup);
            this.triggerSpecEvent()
        }
    }
})
